package cc.rengu.igas.bomp.biz.service.impl;

import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.facade.BompManualAdjustAcctService;
import cc.rengu.igas.bomp.facade.request.ManualAccoutingStatusQueryRequest;
import cc.rengu.igas.bomp.facade.request.ManualAdjustmentRequest;
import cc.rengu.igas.bomp.facade.request.ManualPaymentRequest;
import cc.rengu.igas.bomp.facade.request.MompPaymentRequest;
import cc.rengu.igas.bomp.facade.response.ManualAccoutingStatusQueryResponse;
import cc.rengu.igas.bomp.facade.response.ManualAdjustmentResponse;
import cc.rengu.igas.bomp.facade.response.ManualPaymentResponse;
import cc.rengu.igas.bomp.facade.response.MompPaymentResponse;
import cc.rengu.igas.bomp.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * 手工调账服务实现类
 * Created by 王成 on 2020/3/21
 */
public class BompManualAdjustAcctServiceImpl implements BompManualAdjustAcctService {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<ManualPaymentResponse> manualPayment(ManualPaymentRequest manualPaymentRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<ManualPaymentResponse> result = new Result<>();
        ManualPaymentResponse manualPaymentResponse = new ManualPaymentResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(manualPaymentRequest, ManualPaymentResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((ManualPaymentResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("手工挂账处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                manualPaymentResponse.setRespCode(((BizException) e).getCode());
                manualPaymentResponse.setRespDesc(e.getMessage());
            } else {
                manualPaymentResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                manualPaymentResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<ManualAdjustmentResponse> manualAdjustment(ManualAdjustmentRequest manualAdjustmentRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<ManualAdjustmentResponse> result = new Result<>();
        ManualAdjustmentResponse manualAdjustmentResponse = new ManualAdjustmentResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(manualAdjustmentRequest, ManualAdjustmentResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((ManualAdjustmentResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("手工调账处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                manualAdjustmentResponse.setRespCode(((BizException) e).getCode());
                manualAdjustmentResponse.setRespDesc(e.getMessage());
            } else {
                manualAdjustmentResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                manualAdjustmentResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<ManualAccoutingStatusQueryResponse> manualAccoutingStatusQuery(ManualAccoutingStatusQueryRequest manualAccoutingStatusQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<ManualAccoutingStatusQueryResponse> result = new Result<>();
        ManualAccoutingStatusQueryResponse manualAccoutingStatusQueryResponse = new ManualAccoutingStatusQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(manualAccoutingStatusQueryRequest, ManualAccoutingStatusQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((ManualAccoutingStatusQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("手工调账处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                manualAccoutingStatusQueryResponse.setRespCode(((BizException) e).getCode());
                manualAccoutingStatusQueryResponse.setRespDesc(e.getMessage());
            } else {
                manualAccoutingStatusQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                manualAccoutingStatusQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }


    @Override
    public Result<MompPaymentResponse> mompPayment(MompPaymentRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MompPaymentResponse> result = new Result<>();
        MompPaymentResponse response = new MompPaymentResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, MompPaymentResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((MompPaymentResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("营销打款处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
